/**
 * 
 */
package com.yonder.agenda.rest;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.yonder.agenda.entity.Contact;
import com.yonder.agenda.service.ContactService;

/**
 * REST services for {@link Contact} entity.
 * 
 * @author CosminS
 * @since Jan 29, 2014
 */
@Component
@Path("contacts")
public class ContactRestService {

	/**
	 * Injected {@link ContactService}.
	 */
	@Autowired
	private ContactService contactService;

	/**
	 * .
	 * 
	 * @param persisted
	 *            .
	 * @return .
	 */
	@Path("/addContact")
	@PUT
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public Contact addContact(Contact persisted) {
		persisted = contactService.create(persisted);
		return persisted;
	}

	/**
	 * .
	 * 
	 * @return .
	 */
	@GET
	@Path("/getContacts")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getAllContacts() {
		List<Contact> contactsList = contactService.findAll();
		return Response.ok(contactsList).build();
	}

	/**
	 * .
	 * 
	 * @param contactId
	 *            .
	 * 
	 * @return .
	 */
	@GET
	@Path("/getContact/{contactId}")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getContactById(@PathParam(value = "contactId") Long contactId) {
		Contact contact = contactService.findById(contactId);
		return Response.ok(contact).build();
	}

	/**
	 * .
	 * 
	 * @param firstName
	 *            .
	 * @param lastName
	 *            .
	 * @return .
	 */
	@GET
	@Path("/getContact")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getContactByIdAndFirstName(@QueryParam(value = "firstName") String firstName, @QueryParam(
			value = "lastName") String lastName) {
		Contact contact = contactService.findByFirstNameAndLastName(firstName, lastName);
		return Response.ok(contact).build();
	}
	
	/**
	 * .
	 * 
	 * @param persisted
	 *            .
	 * @return .
	 */
	@Path("/addContact")
	@PUT
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public Contact addContactD(Contact persisted) {
		persisted = contactService.create(persisted);
		return persisted;
	}

}